const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const { HotModuleReplacementPlugin } = require('webpack');

// console.log(require('webpack'))

// const name = '[name]-[contenthash:6]'
const name = '[name]'
module.exports = {
    mode: '.env.development',
    entry: {
      main: path.join(__dirname, 'svgRender.vue')
    },
    output: {
        path: path.join(__dirname, 'dist'),
        filename: `js/${name}.js`
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                // use 有字符串，对象，数组字符串，数组对象的写法
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
            },
            {
                test: /\.less$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'less-loader']
            },
            {
                test: /\.(png|jpe?g|gif)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        name: `${name}.[ext]`,
                        // 图片的输出路径
                        outputPath: 'img/',
                        // 图片的引用位置，css中如何引用图片
                        // publicPath: '../',
                        limit: 1024 * 2
                    }
                }]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                use: {
                    loader: 'file-loader',
                    options: {
                        name: `${name}.[ext]`,
                        outputPath: 'fonts/'
                    }
                }
            },
            // 自定义loader
            {
                test: /\.js$/,
                use: [
                    {
                        loader: path.join(__dirname, 'loader/messageLoader'),
                        options: {
                            name: 'messageLoader'
                        }
                    }
                ]
            }
        ]
    },
    // devtool: 'cheap-module-source-map',
    devServer: {
        // contentBase: 'dist'
        port: 8080, // 端口号
        hot: true,
        open: true, // 自动打开浏览器，
        /*proxy: {
            '/dev-api': {
                target: 'http://www.test.com',
                changeOrigin: true, // 是否允许跨域
                logLevel: 'debug',
                pathRewrite: {
                    '/dv-admin-api': ''
                }
            }
        }*/
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            template: path.join(__dirname, '浏览器插件.html'),
            filename: "浏览器插件.html",
            inject: 'body'
        }),
        // 如果是多页面的话，值chunks去关联html需要引用的模块
        /*new HtmlWebpackPlugin({
            template: path.join(__dirname, '浏览器插件.html'),
            filename: "list.html",
            inject: 'body',
            chunks: ["list"] // 入口名称
        }),*/
        new MiniCssExtractPlugin({
            filename: `css/${name}.css`
        }),
        new HotModuleReplacementPlugin()
    ]
}